package com.future.hedongjun.mine.adapter;

import android.graphics.Color;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.future.baselib.utils.GlideUtils;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.mall.bean.OrderDetail;

import java.util.List;

/**
 * 订单适配器
 * Created by jniu on 2017/9/6.
 */

public class OrderAdapter extends BaseQuickAdapter<OrderDetail, BaseViewHolder> implements BaseQuickAdapter.OnItemChildClickListener {

    private MyApp app;
    final int colorGreen = Color.parseColor("#FF262C");
    final int colorGray = Color.parseColor("#999999");

    /*
    待支付
    待配送
    配送中
    待自取
    已完成
    已取消
    退款审核中
    退款成功
     */

    public OrderAdapter(@Nullable List<OrderDetail> data) {
        super(R.layout.recycler_item_order, data);
        app = MyApp.getInstance();
        setOnItemChildClickListener(this);
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderDetail item) {
        //绑定数据
        bindData(helper, item);
        //根据状态设置视图
        setTypeView(helper, item);
    }


    private void setTypeView(BaseViewHolder helper, OrderDetail item) {
        helper.setTextColor(R.id.tv_type, colorGreen);
        helper.setVisible(R.id.item_btn, false);
        switch (item.status) {
            case 0://待支付
                helper.setText(R.id.tv_type, "待支付");
                helper.setVisible(R.id.item_btn, true);
                helper.setText(R.id.item_btn, "立即支付");
                break;
            case 1://待发货
                helper.setText(R.id.tv_type, "待发货");
                break;
            case 2://待收货
                helper.setText(R.id.tv_type, "待收货");
                helper.setVisible(R.id.item_btn, true);
                helper.setText(R.id.item_btn, "确认收货");
                break;
            case 3://待评价
                helper.setText(R.id.tv_type, "待评价");
                break;
            case 4://已完成
                helper.setText(R.id.tv_type, "已完成");
                helper.setTextColor(R.id.tv_type, colorGray);
                break;
            case 5://已取消
                helper.setText(R.id.tv_type, "已取消");
                helper.setTextColor(R.id.tv_type, colorGray);
                break;
            case 6://退款审核中
                helper.setText(R.id.tv_type, "退款审核中");
                break;
            case 7://退款成功
                helper.setText(R.id.tv_type, "退款成功");
                helper.setTextColor(R.id.tv_type, colorGray);
                break;
        }
    }

    private void bindData(BaseViewHolder helper, OrderDetail item) {
        if (item.goodsBeanList == null) {
            return;
        }
        int goodsCount = 0;
        for (OrderDetail.GoodsBean bean : item.goodsBeanList) {
            goodsCount += bean.count;
        }
        helper.setText(R.id.tv_order_id, "订单编号" + item.orderNum)//订单编号
                .setText(R.id.tv_count, "共" + goodsCount + "件")//商品数量
                .setText(R.id.tv_price, String.format("¥%.2f", item.amount - item.couponPrice))//价格
                .addOnClickListener(R.id.item_btn);
        if (item.goodsBeanList.size() > 1) {//多图
            helper.setVisible(R.id.item_iv_2, true);
            helper.setVisible(R.id.tv_name, false);
            helper.setVisible(R.id.tv_tib, false);
            GlideUtils.loadImage(mContext, item.goodsBeanList.get(0).imgUrl, (ImageView) helper.getView(R.id.item_iv_1));//图一
            GlideUtils.loadImage(mContext, item.goodsBeanList.get(1).imgUrl, (ImageView) helper.getView(R.id.item_iv_2));//图二
        } else {//单图
            helper.setVisible(R.id.item_iv_2, false);
            helper.setVisible(R.id.tv_name, true);
            helper.setVisible(R.id.tv_tib, true);
            helper.setText(R.id.tv_name, item.goodsBeanList.get(0).title);//商品名
            helper.setText(R.id.tv_tib, item.goodsBeanList.get(0).sku);//备注
            GlideUtils.loadImage(mContext, item.goodsBeanList.get(0).imgUrl, (ImageView) helper.getView(R.id.item_iv_1));//图一
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        String txt = ((Button) view).getText().toString();
        if (txt.equals("立即支付")) {

        } else if (txt.equals("确认收货")) {

        }
    }
}
